package com.hsgene.discount.controller;

import com.hsgene.common.ClientType;
import com.hsgene.common.util.exception.CommonErrorCode;
import com.hsgene.common.util.resolver.FromSession;
import com.hsgene.common.util.result.ApiResult;
import com.hsgene.common.util.support.LimitHelper;
import com.hsgene.common.util.support.OrderHelper;
import com.hsgene.common.util.support.Pagination;
import com.hsgene.common.util.web.session.UserInfo;
import com.hsgene.coupon.dto.GeneticUserDiscountDto;
import com.hsgene.discount.condition.DiscountCondition;
import com.hsgene.discount.domain.GeneticDiscount;
import com.hsgene.discount.service.DiscountService;
import com.hsgene.discount.service.GeneticDiscountService;
import com.hsgene.discount.service.GeneticUserDiscountService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * @desc:优惠券
 * @author: fuxiang
 * @date: 2018/10/11 14:48
 */
@RequestMapping("/v1.0/genetic/testing/discounts")
@RestController
public class DiscountController {

    @Autowired
    private DiscountService discountService;

    @Resource
    private GeneticDiscountService geneticDiscountService;

    @Resource
    private GeneticUserDiscountService geneticUserDiscountService;

    /**
     * 添加优惠券
     *
     * @param
     * @return
     */
    @PostMapping
    public ApiResult<GeneticDiscount> addDiscount(@RequestBody GeneticDiscount geneticDiscount) {
        return discountService.addDiscount(geneticDiscount);
    }

    /**
     * 删除
     *
     * @param
     * @return
     */
    @DeleteMapping("{id}")
    public ApiResult deleteDiscount(@PathVariable("id") String id) {
        return discountService.deleteDiscount(id);
    }

    /**
     * 批量删除
     *
     * @param
     * @return
     */
    @DeleteMapping
    public ApiResult deleteDiscountMore(@RequestBody List<GeneticDiscount> discountList) {
        return discountService.deleteDiscountMore(discountList);
    }

    /**
     * 查询优惠券类表
     *
     * @param platform 客户端类型
     * @param userId   用户id
     * @param orderBy  排序参数
     * @param isAsc    是否升序
     * @param page     页号
     * @param size     每页大小
     * @return 优惠券列表
     */
    @GetMapping
    public ApiResult queryDiscount(
            @RequestParam(name = "platform", defaultValue = "1") Integer platform,
            @RequestParam(name = "userId", required = false) String userId,
            @RequestParam(name = "orderBy", required = false) String orderBy,
            @RequestParam(name = "isAsc", defaultValue = "false") Boolean isAsc,
            @RequestParam(name = "page", defaultValue = "1") int page,
            @RequestParam(name = "size", defaultValue = "10") int size,
            @FromSession UserInfo userInfo) {

        if (ClientType.CONSOLE.getCode().equals(platform)) {
            DiscountCondition condition = new DiscountCondition();
            condition.setLimitHelper(LimitHelper.create(page, size));
            condition.setOrderHelper(OrderHelper.create("create_date_time", isAsc));
            return discountService.getList(condition);
        } else if (ClientType.APPLETS.getCode().equals(platform)) {
            userId = StringUtils.isBlank(userId) ? (userInfo == null ? null : userInfo.getUserId()) : userId;
            return geneticDiscountService.queryEffectiveDiscount(userId);
        } else {
            return ApiResult.fail(CommonErrorCode.CLIENT_TYPE_EXCEPTION_NOT_SUPPORTED);
        }
    }

    /**
     * 查询用户的优惠券列表
     *
     * @param clientDiscountType 客户端优惠券分类：0-全部，1-待使用，2-已使用，3-已过期
     * @param orderBy            排序参数：discountPrice-优惠券价格，deadline-截止时间，receiveTime-领取时间
     * @param isAsc              是否升序：true-升序，false-降序
     * @param pageNo             页号
     * @param pageSize           每页大小
     * @return 优惠券列表
     */
    @GetMapping("customer")
    public ApiResult<Pagination<GeneticUserDiscountDto>> queryUserDiscount(
            @RequestParam(name = "clientDiscountType", defaultValue = "0") Integer clientDiscountType,
            @RequestParam(name = "orderBy", defaultValue = "discountPrice") String orderBy,
            @RequestParam(name = "isAsc", defaultValue = "false") Boolean isAsc,
            @RequestParam("page") int pageNo,
            @RequestParam("size") int pageSize,
            @FromSession UserInfo userInfo) {
        return geneticUserDiscountService.queryUserDiscount(
                userInfo.getUserId(), clientDiscountType, OrderHelper.create(orderBy, isAsc), LimitHelper.create(pageNo, pageSize));
    }

}
